'use strict';
/**
 * 计算佣金
 * @param {Object} level 等级
 * @param {Number} amount 消费金额
 * @param {Array} arr 分润等级数组
 * @param {Boolean} is_indirect 是否间推  默认false
 */
function getCommission(level, amount, arr, is_indirect) {
	let commission = 0, geadeData= {};
	switch(level) {
		case 1:
			geadeData = arr.find(item => item.grade === level);
			commission = Number(amount) * (is_indirect? geadeData.earnings_indirect : geadeData.earnings);
			break
		case 2:
			geadeData = arr.find(item => item.grade === level);
			commission = Number(amount) * (is_indirect? geadeData.earnings_indirect : geadeData.earnings);
			break
		case 3:
			geadeData = arr.find(item => item.grade === level);
			commission = Number(amount) * (is_indirect? geadeData.earnings_indirect : geadeData.earnings);
			break
		default:
			break
	}
	return commission
}

module.exports = {
	/**
	 * 修改订单
	 * @url admin/order/sys/update 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} _id  ID
	 * @param {String} order_no  订单编号
	 * @param {String} activate_code  激活码
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam, uniIdToken } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { _id } = data;

		delete data["pl_table_level"];
		delete data["userInfo"];
		delete data["uid"];
		delete data["_id"];
		delete data["order_no"];
		
		let configData = await vk.callFunction({
			url: 'admin/system_config/sys/getDetails',
			data: {
				type: "mp-weixin"
			},
			uniIdToken: uniIdToken
		});
		
		let dbName = "db-order";
		
		if(!vk.pubfn.isNull(configData)) { // 判断是否获取配置信息
			let dbRes = await vk.baseDao.findById({
				dbName,
				id: _id
			})
			if(!vk.pubfn.isNull(dbRes)) { // 获取订单信息
				let userData = await vk.baseDao.findById({ // 获取订单用户信息
					dbName: "uni-id-users",
					id: dbRes.uid
				})
				if(!vk.pubfn.isNull(userData)) {
					switch(userData.partner_grade) {
						case 4:
							break
						case 3:
							console.log('//////////////////', userData)
							let commission = getCommission(2, dbRes.pay_money, configData.data.partner_list)
							
							break
						case 2:
							let commission = getCommission(1, dbRes.pay_money, configData.data.partner_list)
							let userData1 = await vk.baseDao.updateAndReturn({
								dbName: "uni-id-users",
								whereJson: {
									my_invite_code: userData.superior_code
								},
								dataJson: {
									balance_stay: _.inc(commission)
								}
							})
							if(!vk.pubfn.isNull(userData1)) {
								let earningsLog = await vk.baseDao.add({
									dbName: "db-earnings-log",
									dataJson: {
										uid: userData1._id,
										change_money: commission,
										change_money_later: userData1.balance_stay,
										desc: "分润奖励",
										type: 1, // 1收益  2支出
										status: 0
									}
								})
							}
							break
						default:
							break
					}
				}
			}
		}
		
		// res.num = await vk.baseDao.updateById({
		// 	dbName,
		// 	id: _id,
		// 	dataJson: data
		// })
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
